﻿using System;
using System.Linq;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.DataLayer.Infra;
using ProjetoComperio.Framework.Extensions;

namespace ProjetoComperio.BusinessLogicLayer
{

    public class EducationalITApplicationBLL : ComperioDataObject
    {
        /// <summary>
        /// This method returns all EducationalITApplications
        /// </summary>
        /// <returns></returns>
        public IQueryable<EducationalITApplication> RetrieveEducationalITApplications()
        {
            return this.DbContext.Contributions.OfType<EducationalITApplication>().AsQueryable();
        }
        
        /// <summary>
        /// This method returns all EducationalITApplications related to a user
        /// </summary>
        /// <param name="academicPersonID"></param>
        /// <returns></returns>
        public IQueryable<EducationalITApplication> RetrieveEducationalITApplications(int academicPersonID)
        {
            return new ContributionBLL().RetrieveAcademicPersonContributions(academicPersonID).OfType<EducationalITApplication>();
        }

        public void Create(EducationalITApplication educationalITApplication)
        {
            this.DbContext.Contributions.Add(educationalITApplication);
            DbContext.SaveChanges();
        }

        public void Edit(EducationalITApplication educationalITApplication)
        {
            EducationalITApplication original = this.RetrieveEducationalITApplication(educationalITApplication.ContributionID);
            original.CopyValuesFrom(educationalITApplication);
            DbContext.SaveChanges();

        }

        /// <summary>
        /// This method retrieves an specific EducationITApplication
        /// </summary>
        /// <param name="educationalITApplicationID"></param>
        /// <returns></returns>
        public EducationalITApplication RetrieveEducationalITApplication(Int32 educationalITApplicationID)
        {
            return DbContext.Contributions.OfType<EducationalITApplication>().Where(educationalITApplication => educationalITApplication.ContributionID == educationalITApplicationID).FirstOrDefault();
        }

        /// <summary>
        /// This method returns the educationalITApplications registered on the last week
        /// </summary>
        /// <returns></returns>
        public IQueryable<EducationalITApplication> RetrieveRecentEducationalITApplications()
        {
            var dateFromLastWeek = DateTime.Now.AddDays(-7);
            return DbContext.Contributions.OfType<EducationalITApplication>().Where(educationalITApplication => educationalITApplication.RegisterDate >= dateFromLastWeek);
        }
    }
}
